<template>
  <div style="width: 100%">
    <!-- logo -->
    <div style="width: 50%; height: 100%; float: left; margin-top: 15px">
      <a id="logo" href="" @click.prevent="goIndex"> 就爱科技</a>
    </div>
    <!-- 登录和二维码 -->
    <div
      style="
        width: 50%;
        height: 62.23px;
        justify-content: flex-end;
        align-items: center;
        display: flex;
      "
    >
      <!-- 二维码 -->
      <span>
        <el-popover
          ref="popover"
          placement="bottom"
          width="230"
          trigger="hover"
        >
          <slot name="reference">
            <div>
              <li class="picture">
                <img src="../assets/logo.png" alt="" /><br />公众号
              </li>
              <li class="picture">
                <img src="../assets/logo.png" alt="" /><br />微信群
              </li>
              <li class="picture">
                <img src="../assets/logo.png" alt="" /><br />QQ群
              </li>
            </div>
          </slot>
        </el-popover>
        <i class="el-icon-s-grid" v-popover:popover></i
      ></span>
      <!-- 分割线 -->
      <el-divider direction="vertical"></el-divider>
      <!-- 登录按钮 -->
      <span v-if="token == false">
        <el-button type="text" @click="login()" style="padding 0px;margin:0px"
          >登录</el-button
        >
        <!-- 登录 -->
        <el-dialog
          title="登录"
          :visible.sync="dialogFormVisible"
          width="540px"
          style="text-align: center"
        >
          <!-- 注册 -->

          <el-dialog
            title="注册"
            :visible.sync="innerVisible"
            width="540px"
            style="text-align: center"
            append-to-body
          >
            <el-form :model="registerform">
              <el-form-item label="账号" :label-width="formLabelWidth">
                <el-col :span="18">
                  <el-input
                    v-model="registerform.Username"
                    autocomplete="off"
                  ></el-input>
                </el-col>
              </el-form-item>
              <el-form-item label="密码" :label-width="formLabelWidth">
                <el-col :span="18">
                  <el-input
                    v-model="registerform.Password"
                    autocomplete="off"
                  ></el-input>
                </el-col>
              </el-form-item>
              <el-form-item label="确认密码" :label-width="formLabelWidth">
                <el-col :span="18">
                  <el-input
                    v-model="registerform.ConfirmPassword"
                    autocomplete="off"
                  ></el-input>
                </el-col>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer" style="">
              <el-button type="primary" @click="innerVisible = false"
                >取 消</el-button
              >
              <el-button
                type="primary"
                @click="register()"
                style="margin-right: 180px"
                >注 册</el-button
              >
            </div>
          </el-dialog>
          <!-- 注册结束-->
          <el-form :model="form">
            <el-form-item label="账号" :label-width="formLabelWidth">
              <el-col :span="18">
                <el-input v-model="form.Username" autocomplete="off"></el-input>
              </el-col>
            </el-form-item>
            <el-form-item label="密码" :label-width="formLabelWidth">
              <el-col :span="18">
                <el-input v-model="form.Password" autocomplete="off"></el-input>
              </el-col>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="dialogFormVisible = false"
              >取 消</el-button
            >
            <el-button
              type="primary"
              @click="submitForm()"
              style="margin-right: 180px"
              >登 录</el-button
            >
            <br />
            <a id="register" href="#" @click.prevent="innerVisible = true"
              >没有账号，去注册</a
            >
          </div>
        </el-dialog>
      </span>
      <!-- 注销 -->
      <span v-else>
        <el-dropdown>
          <span class="el-dropdown-link">
            {{ Username }}<i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="UpdatePassword()"
              >修改密码</el-dropdown-item
            >
            <el-dropdown-item @click.native="handleLogout"
              >注销</el-dropdown-item
            >
          </el-dropdown-menu>
        </el-dropdown>
        <!-- 修改密码 -->
        <el-dialog
          title="修改密码"
          :visible.sync="updatePwd"
          width="540px"
          style="text-align: center"
        >
          <el-form :model="Updateform">
            <el-form-item label="用户名" :label-width="formLabelWidth">
              <el-col :span="18">
                <el-input
                  v-model="Updateform.Username"
                  autocomplete="off"
                ></el-input>
              </el-col>
            </el-form-item>
            <el-form-item label="旧密码" :label-width="formLabelWidth">
              <el-col :span="18">
                <el-input
                  v-model="Updateform.OldPassword"
                  autocomplete="off"
                ></el-input>
              </el-col>
            </el-form-item>
            <el-form-item label="新密码" :label-width="formLabelWidth">
              <el-col :span="18">
                <el-input
                  v-model="Updateform.NewPassword"
                  autocomplete="off"
                ></el-input>
              </el-col>
            </el-form-item>
            <el-form-item label="确认密码" :label-width="formLabelWidth">
              <el-col :span="18">
                <el-input
                  v-model="Updateform.ConfirmPassword"
                  autocomplete="off"
                ></el-input>
              </el-col>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="updatePwd = false"
              >取 消</el-button
            >
            <el-button
              type="primary"
              @click="UpdateOk()"
              style="margin-right: 180px"
              >修 改</el-button
            >
          </div>
        </el-dialog>
      </span>
    </div>
  </div>
</template>

<script>
import { getByUser, UpdateUser, CreateUser } from '../api/user'
export default {
  data () {
    return {
      dialogFormVisible: false,
      form: {
        Username: ' ',
        Password: ' ',
      },
      Updateform: {
        Username: '',
        OldPassword: '',
        ConfirmPassword: '',
        NewPassword: '',
      },
      registerform: {
        Username: '',
        Password: '',
        ConfirmPassword: ''
      },
      Username: '',
      formLabelWidth: "120px",
      innerVisible: false,
      token: false,
      updatePwd: false,
    };
  },
  methods: {
    goIndex () {
      this.$router.push("/");
    },
    //注销
    handleLogout () {
        this.Username == " ";
        console.log(this.Username);
        localStorage.removeItem("accessToken"),
        localStorage.removeItem("refreshToken"),
        localStorage.removeItem("id")
        this.$router.push('/')
        console.log(this.token);

    },
    getToken(){
        if(window.localStorage.getItem("accessToken")==null){
          this.token==false;
        }else{
          this.token==true;
        }
    },
    login () {
      this.dialogFormVisible = true;
    },
    //登录
    submitForm () {
      if (this.form.Password == '' || this.form.Username == '') {
        alert("任意输入框不能为空");
      }
      else {
        this.form.Username = this.form.Username.trim();
        this.form.Password = this.form.Password.trim();
        getByUser(this.form).then((data) => {
          console.log(data.data);
          console.log(this.form);
          if (data.data.code === 1002) {
            alert("抱歉，账号或密码错误，请确认后重试")
          }
          else {
            alert("登录成功");
            localStorage.setItem("accessToken", data.data.data.token);
            localStorage.setItem("refreshToken", data.data.data.refreshToken);
            localStorage.setItem("id",data.data.data.id);
            this.$router.push("/");
            this.token = true;
            this.Username = this.form.Username
          }


        }).catch()

      }
    },
    UpdatePassword () {
      this.updatePwd = true;
    },
    //注册
    register () {
      if (this.registerform.Username == '' || this.registerform.Password == '' || this.registerform.ConfirmPassword == '') {
        alert("任意输入框不能为空");
      }
      else {
        if (this.registerform.Password.trim() != this.registerform.ConfirmPassword.trim()) {
          alert("两次密码需要一致");
          console.log(this.registerform);
        }
        else {
          CreateUser(this.registerform).then(({ data }) => {
            console.log(data);
            this.innerVisible = false;

          }).catch(({ res }) => {
            console.log(res);
          })
        }
      }

    },
    //修改密码
    UpdateOk () {
      if (this.Updateform.ConfirmPassword == '' || this.Updateform.NewPassword == '' || this.Updateform.Username == '' || this.Updateform.OldPassword == '') {
        alert("任意输入框不能为空");
      }
      else {
        if (this.Updateform.ConfirmPassword !== this.Updateform.NewPassword) {
          alert("两次密码需要一致");
        }
        else {
          UpdateUser(this.Updateform).then((data) => {

            console.log(data);
            alert("修改成功");
            this.updatePwd = false;

          }).catch()
        }
      }


    }
  },
    mounted:function(){
      this.getToken();
      console.log(this.token);
    }
};
</script>

<style scoped>
#logo {
  text-decoration: none;
  font-size: 25px;
  color: black;
  font-family: '楷体';
}
#register {
  text-decoration: none;
  font-size: 5px;
  color: red;
}
.picture {
  float: left;
  height: 80px;
  text-align: center;
  font-size: 15px;
  list-style: none;
  margin-left: 10px;
}
img {
  width: 60px;
  height: 60px;
  margin-right: 5px;
}
.el-divider {
  background-color: black;
}
.el-dropdown-link {
  font-family: '楷体';
  font-size: 15px;
}
</style>